package com.jjty.dataGenerator.bg.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jjty.dataGenerator.bg.entity.SchoolInfo;
import com.jjty.dataGenerator.bg.service.SchoolService;
import com.jjty.dataGenerator.common.ApiResult;
import com.jjty.dataGenerator.common.PageParamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author leikk
 * @since 2025-03-18
 */
@RestController
@RequestMapping("/bg/school")
public class SchoolController {

    @Autowired
    private SchoolService schoolService;

    /**
     * 添加学校
     * @param school 学校信息
     * @return 操作结果
     */
    @RequestMapping("/add")
    public ApiResult addSchool(SchoolInfo school) {
        // 检查ecid是否已经存在
        boolean Exists = schoolService.isEcidExists(school.getEcid());
        if (Exists) {
            return ApiResult.doFailure("添加学校失败，ecid已存在");
        }
        boolean result = schoolService.save(school);
        if (result) {
            return ApiResult.doSuccess("添加学校成功",school);
        } else {
            return ApiResult.doFailure("添加学校失败");
        }
    }

    /**
     * 删除学校
     * @param id 学校ID
     * @return 操作结果
     */
    @RequestMapping("/delete")
    public ApiResult deleteSchool(Integer id) {
        boolean result = schoolService.removeById(id);
        if (result) {
            return ApiResult.doSuccess("删除学校成功");
        } else {
            return ApiResult.doFailure("删除学校失败");
        }
    }

    /**
     * 修改学校信息
     * @param school 学校信息
     * @return 操作结果
     */
    @RequestMapping("/update")
    public ApiResult updateSchool(SchoolInfo school) {
        // 检查ecid是否已经存在
        boolean Exists = schoolService.isEcidExists(school.getEcid());
        if (Exists) {
            return ApiResult.doFailure("修改学校失败，ecid已存在");
        }
        boolean result = schoolService.updateById(school);
        if (result) {
            return ApiResult.doSuccess("修改学校信息成功",school);
        } else {
            return ApiResult.doFailure("修改学校信息失败");
        }
    }

    /**
     * 分页查询所有学校
     * @param request HttpServletRequest对象，用于获取分页参数
     * @return 学校分页列表
     */
    @RequestMapping("/queryByPage")
    public ApiResult listSchools(HttpServletRequest request, SchoolInfo param, String keyword) {
        Page page = PageParamReader.read(request);
        page = schoolService.getSchoolInfoPage(page,param,keyword);
        return ApiResult.querySuccess(page);
    }

    /**
     * 根据ID查询学校
     * @param id 学校ID
     * @return 学校信息
     */
    @RequestMapping("/get")
    public ApiResult getSchoolById(Integer id) {
        SchoolInfo school = schoolService.getById(id);
        if (school != null) {
            return ApiResult.querySuccess(school);
        } else {
            return ApiResult.queryFailure();
        }
    }

}

